package cn.tedu._03vehice.controller;

import cn.tedu._03vehice.base.response.JsonResult;
import cn.tedu._03vehice.base.utils.CacheUtil;
import cn.tedu._03vehice.mapper.NoticeMapper;
import cn.tedu._03vehice.pojo.dto.NoticeAddParam;
import cn.tedu._03vehice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.License;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Api(tags = "03.公告管理模块")
@RestController
@RequestMapping("v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private CacheUtil cacheUtil;
    @PostMapping("list")
    @ApiOperation("首页公告展示")
    @ApiOperationSupport(order = 10)
    public JsonResult listNotice(){
        /*
           设计key-value
             key: notice_index
             value: List<NoticeListVO>
           1.到Redis中查询首页的数据;
           2.如果Redis中存在，直接返回;
           3.如果Redis中不存在
            3.1到MySQL数据库中
            3.2
            3.3
         */
        Serializable noticeIndex = cacheUtil.get("notice_index");
        if (noticeIndex != null){
            log.debug("首页数据来自Redis数据库...");
            return JsonResult.ok(noticeIndex);
        }
        log.debug("首页数据来自Mysql数据库...");
        List<NoticeListVO> noticeListVOS = noticeMapper.list();
        cacheUtil.setExpire("notice_index", (Serializable) noticeListVOS, 30);
        return JsonResult.ok(noticeListVOS);
    }
}
